using UnityEngine;

namespace Yoozoo.Modules.InnerCity
{
    public class RotateRect
    {
	    public Vector2[] world_corners = new Vector2[4];

	    public Vector2[] local_corners = new Vector2[4];

	    private const float sin45 = 0.70721f;
	    
	    private const float cos45 = 0.70721f;

		public RotateRect(Vector2 pos, Vector2 size)
		{
			this.UpdateRect(pos, size);
		}

		public void UpdateRect(Vector2 pos, Vector2 size)
		{
			size /= 2f;
			this.world_corners[0].x = pos.x + size.y * 0.70721f - size.x * 0.70721f;
			this.world_corners[0].y = pos.y + size.y * 0.70721f + size.x * 0.70721f;
			this.world_corners[1].x = pos.x + size.y * 0.70721f + size.x * 0.70721f;
			this.world_corners[1].y = pos.y + size.y * 0.70721f - size.x * 0.70721f;
			this.world_corners[2].x = pos.x - size.y * 0.70721f + size.x * 0.70721f;
			this.world_corners[2].y = pos.y - size.y * 0.70721f - size.x * 0.70721f;
			this.world_corners[3].x = pos.x - size.y * 0.70721f - size.x * 0.70721f;
			this.world_corners[3].y = pos.y - size.y * 0.70721f + size.x * 0.70721f;
			this.local_corners[0].x = this.world_corners[0].x * 0.70721f + this.world_corners[0].y * 0.70721f;
			this.local_corners[0].y = -this.world_corners[0].x * 0.70721f + this.world_corners[0].y * 0.70721f;
			this.local_corners[1].x = this.world_corners[1].x * 0.70721f + this.world_corners[1].y * 0.70721f;
			this.local_corners[1].y = -this.world_corners[1].x * 0.70721f + this.world_corners[1].y * 0.70721f;
			this.local_corners[2].x = this.world_corners[2].x * 0.70721f + this.world_corners[2].y * 0.70721f;
			this.local_corners[2].y = -this.world_corners[2].x * 0.70721f + this.world_corners[2].y * 0.70721f;
			this.local_corners[3].x = this.world_corners[3].x * 0.70721f + this.world_corners[3].y * 0.70721f;
			this.local_corners[3].y = -this.world_corners[3].x * 0.70721f + this.world_corners[3].y * 0.70721f;
		}

		public bool Overlaps(RotateRect rotate_rect)
		{
			bool cross = IsCross(rotate_rect, this);
			if (cross)
			{
				return true;
			}
			return this.IsOverlaps(this, rotate_rect) || this.IsOverlaps(rotate_rect, this);
		}
		
		/// <summary>
		/// 检测是否包含区域(只要有一条边不再区域内就是不包含)
		/// </summary>
		/// <param name="rotate_rect"></param>
		/// <returns></returns>
		public bool Contains(RotateRect rotate_rect)
		{
			for (int num = 0; num != rotate_rect.local_corners.Length; num++)
			{
				if (rotate_rect.local_corners[num].x < this.local_corners[2].x || rotate_rect.local_corners[num].x > this.local_corners[1].x || rotate_rect.local_corners[num].y < this.local_corners[2].y || rotate_rect.local_corners[num].y > this.local_corners[0].y)
				{
					return false;
				}
			}
			return true;
		}
		
		/// <summary>
		/// 检测rect0是否完全覆盖rect1
		/// </summary>
		/// <param name="rect0"></param>
		/// <param name="rect1"></param>
		/// <returns></returns>
		private bool IsOverlaps(RotateRect rect0, RotateRect rect1)
		{
			for (int num = 0; num != rect1.local_corners.Length; num++)
			{
				if (rect0.local_corners[2].x <= rect1.local_corners[num].x && 
				    rect0.local_corners[1].x >= rect1.local_corners[num].x && 
				    rect0.local_corners[2].y <= rect1.local_corners[num].y && 
				    rect0.local_corners[0].y >= rect1.local_corners[num].y)
				{
					return true;
				}
			}
			return false;
		}

		// 检测两rect是否交叉
		public bool IsCross(RotateRect rect0, RotateRect rect1)
		{
			return !(rect0.local_corners[0].x < rect1.local_corners[2].x ||
			 rect0.local_corners[2].y > rect1.local_corners[0].y ||
			 rect1.local_corners[0].x < rect0.local_corners[2].x ||
			 rect1.local_corners[2].y > rect0.local_corners[0].y);
		}
		
    }
}